package com.example.controller;

import com.example.pojo.ConditionsQuery;
import com.example.pojo.DrugCompany;
import com.example.pojo.Patient;
import com.example.pojo.Result;
import com.example.service.PatientService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;

    @RequestMapping("/add")
    public Result patientAdd(@RequestBody Patient patient){
        return patientService.patientAdd(patient);
    }

    @RequestMapping("/edit")
    public Result editPatient(@RequestBody Patient patient){

        return patientService.editPatient(patient);
    }
    @RequestMapping("/getPatientList")
    public Result getPatientList(@RequestBody ConditionsQuery conditionsQuery){

        PageHelper.startPage(conditionsQuery.getPageNum(),conditionsQuery.getPageSize());

        //System.err.println(doctorQuery);
        List<Patient> list=patientService.getPatientList(conditionsQuery);
        // System.err.println(list);

        PageInfo<Patient> pageInfo=new PageInfo<>(list);

        Map<String,Object> resultMap=new HashMap<>();

        //当前页数据
        resultMap.put("list",pageInfo.getList());

        //数据总数
        resultMap.put("total",pageInfo.getTotal());

        return new Result(200,"数据加载成功",resultMap);
    }

    @RequestMapping("delById")
    public Result delPatientById(@RequestBody Map<String,Integer> map){
        Integer id=map.get("id");
        return patientService.delPatientById(id);
    }
    @RequestMapping("/delBatch")
    public Result delBatch(@RequestBody Map<String,String> map){

        String ids = map.get("ids");
        return patientService.delBatch(ids);
    }
}
